import {
    Component,
    ImageAsset,
    Node,
    Sprite,
    SpriteFrame,
    Texture2D,
    UITransform,
    log,
    view,
} from "cc"

export const fitContain = (target: ImageAsset | Texture2D | Sprite | SpriteFrame | Node, component: Component | Node) => {
    if (target instanceof ImageAsset) {
        const { width, height } = view.getVisibleSize()
        const ratio = target.width / target.height
        const bool = width / height > ratio
        log("屏幕宽度", width, "屏幕高度", height, "屏幕分辨率")
        const transform = component.getComponent(UITransform)
        log('*屏幕宽高比*', width / height, '*图片宽高比*', ratio)
        if (bool) {
            transform.setContentSize(height * ratio, height)
        } else {
            transform.setContentSize(width, width / ratio)
        }
    }
}

export const fitCover = (target: ImageAsset | Texture2D | Sprite | SpriteFrame | Node, component: Component | Node) => {
    if (target instanceof ImageAsset || target instanceof SpriteFrame) {
        const { width, height } = view.getVisibleSize()
        const ratio = target.width / target.height
        const bool = width / height > ratio
        const transform = component.getComponent(UITransform)
        log('*屏幕宽高比*', width / height, '*图片宽高比*', ratio)
        log('bool', bool)
        if (bool) {
            transform.setContentSize(width, width / ratio)
        } else {
            transform.setContentSize(height * ratio, height)
        }
    }
}


export const scaleRatio = (target: UITransform, sp: SpriteFrame): { width: number, height: number } => {
    return {
        width: target.width / sp.width,
        height: target.height / sp.height,
    }
}
